package com.langsin.my0628;


import com.langsin.bean.User;
import com.langsin.liuwenqiu.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @version: 1.0
 * @author: Liu Wenqiu
 * @Email: 2651255863@qq.com
 * @date 2022/6/28 16:32
 */
public class LwqTest {

    /*
    一级缓存 存在于一个sqlSession
     */
    @Test
    public void LwqTest1() throws IOException {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

        User user = mapper.queryById(702);
        System.out.println("第一次查询:" + user);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        User user1 = mapper.queryById(702);
        System.out.println("第二次查询:" + user1);


    }

    /*
        二级缓存 需要 我们的对象 能 序列化   , 二级缓存 有可能存入到硬盘里的.
    */
    @Test
    public void LwqTest2() throws IOException {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        SqlSession session1 = sqlSessionFactory.openSession();
        UserMapper mapper1 = session1.getMapper(UserMapper.class);

        User user1 = mapper1.queryById(702);
        System.out.println("第一次查询:" + user1);
        session1.close();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        SqlSession session2 = sqlSessionFactory.openSession();
        UserMapper mapper2 = session2.getMapper(UserMapper.class);
        User user2 = mapper2.queryById(702);
        System.out.println("第二次查询:" + user2);
    }
}

